package com.woniuxy.ssm.controller;

import com.woniuxy.ssm.bean.User;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author linke
 * @version 1.0
 * @className: UserController
 * @description: User 控制器
 * @date 2021/11/18 11:40
 *
 * 用来学习传递参数
 * @RestController 不跳转页面
 */
@RestController
@RequestMapping("/user")
public class UserController {

    /**
     * 常规写法
     * @return
     */
    @RequestMapping("/findOne")
    public ModelAndView findOne(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("msg","页面跳转");

        modelAndView.setViewName("stringView");
        return modelAndView;
    }

    /**
     * @RequestParam 用于解决前后端传递 参数名称不一致
     * 传递参数
     * @return
     * @PathVariable 是spring3.0 的一个新功能：接收请求路径中占位符的值
     * @PathVariable("xxx")
     * 通过 @PathVariable 可以将URL中占位符参数{xxx}绑定到处理器类的方法形参中@PathVariable(“xxx“)
     *
     * @RequestMapping(value=”user/{id}/{name}”)
     * 请求路径：http://localhost:8080/ssm/user/1/james
     */
    @RequestMapping("/findTwo/{id}")
    public ModelAndView findTwo(@RequestParam("uname") String uname, String pwd,@PathVariable("id")int id){
        ModelAndView modelAndView = new ModelAndView();
        System.out.println(id);
        modelAndView.addObject("uname","传入的参数是:"+uname);
        modelAndView.addObject("pwd","传入的参数是:"+pwd);
        modelAndView.setViewName("success");
        return modelAndView;
    }

    /**
     * 传int数组
     * @param ids
     * @return
     */
    @PostMapping ("/add")
    public ModelAndView add(int[] ids){
        ModelAndView modelAndView = new ModelAndView();
        //处理后进行转换
        List<Object> list = new ArrayList<>();
        for (int id : ids) {
            list.add(id);
        }
        modelAndView.addObject("uname","传入的int数组是:"+list);
        modelAndView.setViewName("success");
        return modelAndView;
    }

    /**
     * 传list 数组
     * @param ids
     * @return
     */
    @PostMapping ("/delete")
    public ModelAndView delete(@RequestParam List<Integer> ids){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("ids","传入的集合数组是:"+ids);
        modelAndView.setViewName("success");
        return modelAndView;
    }

    /**
     * map类型传递参数 接收参数
     * 所有形参对象名，和前端字段名无关，
     * 但是前端字段名和对象属性名一致
     * @param user
     * @return
     */
    @PostMapping ("/map")
    public ModelAndView map(@RequestParam Map user){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("user","传入的Map集合数组是:"+user);
        modelAndView.setViewName("mapView");
        return modelAndView;
    }

    /**
     * 封装对象接收
     * @RequestBody 将传递的json转换成对象接收
     * @param user
     * @return
     */
    @PostMapping ("/addUser")
    public ModelAndView addUser(@RequestBody User user){
        ModelAndView modelAndView = new ModelAndView();
        System.out.println(user);
        modelAndView.addObject("user","传入的Map集合数组是:"+user);
        modelAndView.setViewName("mapView");
        return modelAndView;
    }
}
